package com.blackshadowgame.rps.slice;

import com.blackshadowgame.rps.ResourceTable;
import com.blackshadowgame.utils.LogUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.utils.IntervalTimer;

import java.util.ArrayDeque;
import java.util.Queue;

public abstract class BaseSlice extends AbilitySlice {
    private static final String TAG = BaseSlice.class.getSimpleName();
    public String getTag(){
        return TAG;
    }

    private static Queue<String> infoQueue = new ArrayDeque<>();
    private Text txtLog = null;
    private IntervalTimer gameTimer = null;

    protected int getLayoutId(){
        return 0;
    }
    protected int getTextLogId(){
        return 0;
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setActiveSlice(this);

        // 关联UI布局
        super.setUIContent(getLayoutId());

        initUIComponent();
        startGameLoop();
    }

    protected void initUIComponent() {
        LogUtil.info(TAG, "===> 加载并绑定界面组件");

        if(getTextLogId()>0){
            txtLog = (Text)findComponentById(getTextLogId());
        }
        addInfo("加载界面..." + this.getClass().getSimpleName());
    }

    // 显示信息文本
    protected void addInfo(String info){
        addInfo(getTag(), info);
    }

    protected void addInfo(String tag, String info){
        LogUtil.info(tag, "===> " + info);

        infoQueue.add(info);
        if(infoQueue.size()>20){
            infoQueue.remove();
        }

        if(txtLog!=null){
            txtLog.setText(String.join("\n", infoQueue));
        }
    }

    protected void goGamePlaySlice(){
        stopGameLoop();
        present(new GamePlaySlice(), new Intent());
    }

    protected void goHomeSlice(){
        stopGameLoop();
        present(new HomeSlice(), new Intent());
    }

    protected void goWaitSlice(){
        stopGameLoop();
        present(new WaitingSlice(), new Intent());
    }

    private static BaseSlice _activeSlice = null;
    public static void setActiveSlice(BaseSlice slice){
            _activeSlice = slice;
    }

    public static BaseSlice getActiveSlice(){
        return _activeSlice;
    }

    public static void addLog(String tag, String log){
        if(_activeSlice != null){
            _activeSlice.addInfo(log);
        }else{
            LogUtil.info(tag, "===>" + log);
        }
    }

    public void runInMainThread(Runnable task){
        EventRunner runner = EventRunner.getMainEventRunner();
        EventHandler handler = new EventHandler(runner);
        handler.postTask(task);
    }

    private void startGameLoop(){
        if(gameTimer == null){
            gameTimer = new IntervalTimer(1000, 1000) {
                @Override
                public void onInterval(long l) {
                    runInMainThread(new Runnable() {
                        @Override
                        public void run() {
                            onLoop(l);
                        }
                    });
                }

                @Override
                public void onFinish() {
                    gameTimer.schedule();
                }
            };
            gameTimer.schedule();
        }
    }

    private void stopGameLoop(){
        if(gameTimer != null){
            gameTimer.cancel();
            gameTimer = null;
        }
    }

    protected void onLoop(long deltaTime) {

    }
}
